package com.hry.store.cms.controller;

import com.hry.store.cms.common.Const;
import com.hry.store.cms.common.ServerResponse;
import com.hry.store.cms.pojo.HotProduct;
import com.hry.store.cms.pojo.SecondCategory;
import com.hry.store.cms.pojo.TopCategory;
import com.hry.store.cms.service.CategoryService;

import com.hry.store.cms.service.HotProductService;
import com.hry.store.cms.service.SecondCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SecondCategoryService secondCategoryService;
    @Autowired
    private HotProductService hotProductService;

    /**
     * 查询所有一级标题
     *
     * @return
     */
    @RequestMapping("category/getTopCategory")
    @ResponseBody
    public ServerResponse getTopCategory() {
        List<TopCategory> topCategoryList = categoryService.getTopCategory();
        if (!CollectionUtils.isEmpty(topCategoryList)) {

            return ServerResponse.createBySuccess(topCategoryList);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "获取失败");
    }

    /**
     *
     */
    @RequestMapping("category/getTopCategorys")
    public String getTopCategory_2(ModelMap modelMap) {
        List<TopCategory> topCategoryList = categoryService.getTopCategory();
        if (!CollectionUtils.isEmpty(topCategoryList)) {
            modelMap.put("topCategoryList", topCategoryList);
            return "top_category";
        }
        return "error";
    }

    /**
     * 根据一级分类id查询详细内容
     */
    @RequestMapping("category/getByTopCategoryInfo")
    @ResponseBody
    public ServerResponse getByTopCategoryInfo(@RequestParam("topCategoryId") Integer topCategoryId) {
        TopCategory topCategory = categoryService.getByTopCategory(topCategoryId);
        if (topCategory != null) {
            return ServerResponse.createBySuccess(topCategory);
        }
        return ServerResponse.createBySuccess("查询失败", Const.ERROR);
    }


    /**
     * 根据一级分类id删除分类
     */
    @RequestMapping("category/deleteByTopCategoryInfo")
    @ResponseBody
    public ServerResponse deleteByTopCategoryInfo(@RequestParam("topCategoryId") Integer topCategoryId) {
        int result = categoryService.deleteByTopCategory(topCategoryId);
        if (result > 0) {
            return ServerResponse.createBySuccess("删除成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage( Const.ERROR,"删除失败");
    }

    /**
     * 添加一级分类
     */
    @RequestMapping("category/addCategory")
    @ResponseBody
    public ServerResponse addCategory(@RequestParam("topCategoryName") String topCategoryName, @RequestParam("status") Integer status) {
        int resultCount = categoryService.addTopCategory(topCategoryName, status);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "添加失败");
    }



    /**
     * 更新一级分类
     */
    @RequestMapping("category/updateTopCategory")
    @ResponseBody
    public ServerResponse updateTopCategory(@RequestParam("topCategoryName") String topCategoryName, @RequestParam("topCategoryId") Integer topCategoryId,
                                            @RequestParam("status") Integer status) {
        int resultCount = categoryService.updateTopCategoryId(topCategoryId, topCategoryName, status);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("更新成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "更新失败");
    }

    /**
     * 根据一级标题查询二级标题
     */
    @RequestMapping("category/getByTopCategory")
    @ResponseBody
    public ServerResponse getByTopCategory(@RequestParam("topCategoryId") Integer topCategoryId) {
        List<SecondCategory> secondCategoryList = secondCategoryService.getByTopCategory(topCategoryId);
        if (!CollectionUtils.isEmpty(secondCategoryList)) {
            return ServerResponse.createBySuccess(secondCategoryList);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "查询失败");

    }

    /**
     * 添加二级标题
     */
    @RequestMapping("category/addSecondCategory")
    @ResponseBody
    public ServerResponse addSecondCategory(@RequestParam("productName") String productName,@RequestParam("topCategory") Integer topCategory) {
        int resultCount = secondCategoryService.addSecondCategory(topCategory, productName);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "添加失败");
    }


    /**
     * 根据一级标题查询热门商品名称
     */
    @RequestMapping("category/getHotProductByTopCategory")
    @ResponseBody
    public ServerResponse getHotProductByTopCategory(@RequestParam("topCategoryId") Integer topCategoryId) {
        List<HotProduct> hotProducts = hotProductService.getHotProductByTopCategory(topCategoryId);
        if (!CollectionUtils.isEmpty(hotProducts)) {
            return ServerResponse.createBySuccess(hotProducts);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "查询失败");
    }

    /**
     * 添加热门商品名称
     */
    @RequestMapping("category/addHotProductName")
    @ResponseBody
    public ServerResponse addHotProductName (@RequestParam("productName") String productName, @RequestParam("topCategory") Integer topCategory) {
        int resultCount = hotProductService.addSecondCategory(topCategory, productName);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "添加失败");
    }


}
